<?php
namespace app\models\behaviors;

use yii\behaviors\AttributeBehavior;
use yii\db\ActiveRecord;

/**
 * 无限级分类
 * Class SortBehavior
 * @package app\behaviors
 */
class SortBehavior extends AttributeBehavior
{


    public $value;

    public $createdAtAttribute = 'sort';


    public $parentidattribute = 'fid';
    public $idattribute = 'id';
    /**
     * 初始化
     * @inheritdoc
     */
    public function init()
    {
        parent::init();
        //不跳过更新
        $this->skipUpdateOnClean = false;

        if (empty($this->attributes)) {
            $this->attributes = [
                ActiveRecord::EVENT_BEFORE_INSERT => $this->createdAtAttribute,
            ];
        }
    }

    /**
     * 触发事件
     */
    public function events()
    {
        $events = parent::events();
        $events[ActiveRecord::EVENT_BEFORE_DELETE] = 'beforeDelete';
        return $events;
    }

    /**
     * 删除前
     * @param $event
     */
    public function beforeDelete($event)
    {
        /* @var $owner  ActiveRecord */
        $owner = $this->owner;
        $idattribute = $this->idattribute;
        $createdAtAttribute = $this->createdAtAttribute;
        if ($owner->$createdAtAttribute == '') {
            $sort = $owner->$idattribute;
        } else {
            $sort = $owner->$createdAtAttribute . "," . $owner->$idattribute;
        }
        $children = $owner->find()->where("sort like '" . $sort . "%'")->all();
        foreach($children as $child){
            $child->delete();
        }
    }

    /**
     * @param \yii\base\Event $event
     * @return int|mixed
     */
    protected function getValue($event)
    {
        $idattribute = $this->idattribute;
        $parentidattribute = $this->parentidattribute;
        $createdAtAttribute = $this->createdAtAttribute;
        /* @var $owner ActiveRecord */
        if ($this->value === null) {
            $owner = $this->owner;
            if ($owner->$parentidattribute) {
                $parent = $owner->findOne($owner->$parentidattribute);
                //排序
                $sort = $parent->$createdAtAttribute . ',' . $parent->$idattribute;
                return $sort;
            }
            return 0;
        }
        return parent::getValue($event);
    }

    /**
     * 返回树
     * @param $id
     * @param string $attribute
     * @return array
     */
    public function getTree($id,$attribute = 'id')
    {
        /* @var $models ActiveRecord */
        $models = $this->getTreeModels($id);
        $data = [];
        foreach ($models as $model)
        {
            $data[] = $model->$attribute;
        }
        return $data;
    }

    /**
     * 返回树数组
     * @param $id
     * @return array|\yii\db\ActiveRecord[]
     */
    protected function getTreeModels($id)
    {
        /* @var $owner ActiveRecord */
        $owner = $this->owner;
        $idattribute = $this->idattribute;
        $createdAtAttribute = $this->createdAtAttribute;
        $thisModel = $owner->findOne($id);
        $models = $owner->find()->where("{$idattribute} in ({$createdAtAttribute})")->all();
        $models[] = $thisModel;
        return $models;
    }
}